/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.dao.ActivityDao;
import com.jeesite.modules.storage.dao.GoodsDao;
import com.jeesite.modules.storage.entity.Brand;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.entity.Category;
import com.jeesite.modules.storage.dao.CategoryDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 分类Service
 */
@Service
@Transactional(readOnly=true)
public class CategoryService extends CrudService<CategoryDao, Category> {

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private ActivityDao activityDao;

	/**
	 * 获取单条数据
	 * @param category
	 * @return
	 */
	@Override
	public Category get(Category category) {
		return super.get(category);
	}
	
	/**
	 * 查询分页数据
	 * @param category 查询条件
	 * @param category.page 分页对象
	 * @return
	 */
	@Override
	public Page<Category> findPage(Category category) {
		Page<Category> categoryPage = super.findPage(category);
		for (Category item : categoryPage.getList()){
			item.setActivityNum(activityDao.findCountByBrandIdOrCateId(null,item.getId()));
		}
		return categoryPage;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param category
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Category category) {
		category.setGoodsNum(category.getIsNewRecord() ? 0 : goodsDao.findCountByBrandIdOrCateId(null , category.getId()));
		super.save(category);
	}
	
	/**
	 * 更新状态
	 * @param category
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Category category) {
		super.updateStatus(category);
	}
	
	/**
	 * 删除数据
	 * @param category
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Category category) {
		super.delete(category);
	}

	/**
	 * 查找分类列表【全部】
	 * @return 查找分类列表
	 */
	public List<Category> getAllList(){
		return dao.findAllList();
	}

	/**
	 * 查找Id【名称】
	 * @param name 名称
	 * @return 查找Id
	 */
	public String getIdByName(String name){
		return dao.findIdByName(name);
	}

	/**
	 * 统计汇总【分类】
	 * @param type 类型【Hot热播 Sale预告】
	 * @param identity 标识【Today今日 Yesterday昨日 Earlier更早 Tomorrow明日 AfterTomorrow后日】
	 * @return 统计汇总【分类】
	 */
	public List<Map> getActivities(String type, String identity){
		List<Map> maps = new ArrayList<>();

		Map allMap = new HashMap();
		allMap.put("cateId","");
		allMap.put("name","全部");
		allMap.put("num",dao.findActivityCount(type.equals("Hot"),type.equals("Sale"),identity.equals("Today"),identity.equals("Yesterday"),identity.equals("Earlier"),identity.equals("Tomorrow"),identity.equals("AfterTomorrow")));
		maps.add(allMap);

		List<Map> categoryMaps = dao.findActivities(type.equals("Hot"),type.equals("Sale"),identity.equals("Today"),identity.equals("Yesterday"),identity.equals("Earlier"),identity.equals("Tomorrow"),identity.equals("AfterTomorrow"));
		for(Map categoryMap : categoryMaps){
			maps.add(categoryMap);
		}
		return maps;
	}
}